﻿using System;
using System.Collections.Generic;
using System.Text;
using AudioPlayer.Plugin.Storage;
using AudioPlayer.Storage;

namespace AudioPlayer.Plugin {
	/// <summary>
	/// Hosted class for plugins
	/// </summary>
	internal class PluginHost : IHost {
		#region Fields
		private PluginBase _host;//Hosted plugin
		private IUserSettings _settings;//Contains user settings
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes aa empty instance of the <see cref="PluginHost"/> class.
		/// </summary>
		public PluginHost() {

		}
		/// <summary>
		/// Initializes a new instance of the <see cref="PluginHost"/> class.
		/// </summary>
		/// <param name="hostedPlugin">Plugin on which to host.</param>
		public PluginHost(PluginBase hostedPlugin) {
			_host = hostedPlugin;
			_settings = new UserSettings(hostedPlugin.Guid);
		}
		#endregion
		#region Public Voids
		/// <summary>
		/// Finds plugin
		/// </summary>
		/// <typeparam name="TPlugin">The type of the plugin.</typeparam>
		/// <returns>NULL, or array of plugins</returns>
		public IPluginBase<TPlugin>[] Find<TPlugin>() {
			IPluginBase<TPlugin>[] retArray = Config.Plugins.Plugins.FindAll<TPlugin>();
			return retArray;
		}

		/// <summary>
		/// Creates the song.
		/// </summary>
		/// <returns>ISong, or NULL</returns>
		public ISong CreateSong() {
			Song s = new Song();
			s.Guid = Guid.NewGuid();
			return s;
		}

		/// <summary>
		/// Creates the playlist.
		/// </summary>
		/// <returns>IPlaylist, or NULL</returns>
		public IPlaylist CreatePlaylist() {
			Playlist pl = new Playlist();
			pl.UserDefined = true;
			pl.Primary = false;
			return pl;
		}
		#endregion
		#region Property
		/// <summary>
		/// Gets self owner of plugin.
		/// </summary>
		/// <value>NULl, The self.</value>
		public IPluginBase Self {
			get {
				return _host;
			}
		}
		/// <summary>
		/// Gets or sets the hosted plugin.
		/// </summary>
		/// <value>The hosted plugin.</value>
		public PluginBase Host {
			get { return _host; }
			set {
				if (_host != value) {
					_host = value;
					_settings = new UserSettings(value.Guid);
				}
			}
		}
		/// <summary>
		/// Gets all plugins plugins.
		/// </summary>
		/// <value>The plugins.</value>
		public IPluginBase[] Plugins {
			get {
				return Config.Plugins.Plugins.ToArray();
			}
		}

		/// <summary>
		/// Gets the settings.
		/// </summary>
		/// <value>The settings.</value>
		public IUserSettings Settings {
			get {
				return _settings;
			}
		}
		#endregion
	}
}
